package com.flycms.modules.system.service.impl;

import com.flycms.common.utils.RedisUtils;
import com.flycms.modules.system.service.ConfigureService;
import com.flycms.modules.system.dao.SystemDao;
import com.flycms.modules.system.entity.System;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * Biz-Boot, All rights reserved
 * 版权：企业之家网 -- 企业建站管理系统<br/>
 * 开发公司：97560.com<br/>
 *
 * @author 孙开飞
 * @version 1.0 <br/>
 * @Description: ${Description}
 * @email 79678111@qq.com
 * @Date: 10:22 2019/11/25
 */
@Service
public class ConfigureServiceImpl implements ConfigureService {
    @Autowired
    private SystemDao systemDao;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 按系统变量key查询对应的value
     *
     * @param keyCode
     * @return
     */
    @Cacheable(value="system",key="'key_'+#keyCode")
    public String findByKeyCode(String keyCode){
        String key="system::key_"+keyCode;
        ValueOperations<String, System> operations = redisTemplate.opsForValue();
        boolean hasKey = redisTemplate.hasKey(key);
        if (hasKey) {
            System system= operations.get(key);
            if(system != null){
                return system.getKeyValue();
            }
        }
        System system=systemDao.findByKeyCode(keyCode);
        // 写入缓存
        operations.set(key, system, 5, TimeUnit.HOURS);
        if(system != null){
            return system.getKeyValue();
        }
        return null;
    }
}
